package com.example.mcmurphy.startproject_115wxy.model;

import com.example.mcmurphy.startproject_115wxy.Iface.WeatherListener;
import com.example.mcmurphy.startproject_115wxy.Iface.Weatheriface;
import com.example.mcmurphy.startproject_115wxy.Service.Weather51service;
import com.example.mcmurphy.startproject_115wxy.bean.Weatherresult;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Mcmurhy on 2017/3/14.
 */
public class Weather51Model implements Weatheriface{
    private Retrofit retrofit;
    String BASEURL = "http://weather.51wnl.com/weatherinfo/";
    private Weather51service weather51Service;
    public  Weather51Model(){
        retrofit=new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    @Override
    public void getWeather(String cityNumber, final WeatherListener listener) {

        weather51Service=retrofit.create(Weather51service.class);
        Call<Weatherresult> call=weather51Service.getResult(cityNumber,1);

        call.enqueue(new Callback<Weatherresult>() {
            @Override
            public void onResponse(Call<Weatherresult> call, Response<Weatherresult> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<Weatherresult> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });
    }
}